package com.yazdee.searchresult;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SearchResultHandler extends DefaultHandler{

	private boolean in_producttag=false;
	private boolean in_nametag = false;
	private boolean in_skutag = false;
    private boolean in_pricetag = false;
    private boolean in_imageURLtag = false;
    private List resultRow=new ArrayList();
    private SearchResultRow row;
    
    public List getSearchResultRows(){
    	return this.resultRow;
    }
    
    @Override
    public void startDocument() throws SAXException {
    	
    }

    @Override
    public void endDocument() throws SAXException {
    	
    }
    
    @Override
    public void startElement(String namespaceURI, String localName,
              String qName, Attributes atts) throws SAXException {
    	if (localName.equals("product")) {
            this.in_producttag = true;
            row=new SearchResultRow();
    	}else if (localName.equals("name")) {
              this.in_nametag = true;
         }else if (localName.equals("SKU")) {
              this.in_skutag = true;
         }else if (localName.equals("price")) {
              this.in_pricetag = true;
         }else if (localName.equals("imageURL")) {
              this.in_imageURLtag = true;
         }
    }
    
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
              throws SAXException {
    	if (localName.equals("name")) {
            this.in_nametag = false;
       }else if (localName.equals("SKU")) {
            this.in_skutag = false;
       }else if (localName.equals("price")) {
            this.in_pricetag = false;
       }else if (localName.equals("imageURL")) {
            this.in_imageURLtag = false;
       }else if (localName.equals("product")) {
            this.in_producttag = false;
            resultRow.add(row);
       }
    }
    
   @Override
   public void characters(char ch[], int start, int length) {
         if(this.in_nametag){
        	 row.setName(new String(ch,start,length));
         }else if(this.in_skutag){
        	 row.setSKU(new String(ch,start,length));
         }else if(this.in_pricetag){
        	 row.setPrice(new String(ch,start,length));
         }else if(this.in_imageURLtag){
        	 row.setImageURL(new String(ch,start,length));
         }
   } 
}
